import numpy

t1 = numpy.arange(12)
print('t1---', t1, '----', t1.shape)

t2 = numpy.array([[1, 2, 3], [4, 5, 6]])
print('t2---', t2, '----', t2.shape)

t3 = numpy.arange(24).reshape((2, 3, 4))
print('t3---', t3)

t4 = t3.reshape(4, 6)
print('t4---', t4)

t5 = t3.flatten()
print('t5---', t5)

# 转置
print(t2.T)
print(t2.transpose())
print(t2.swapaxes(1, 0))

print('取行', t4[2])
print('取连续多行', t4[2:])
print('取不连续多行', t4[[1, 3]])
print('取列', t4[:, 0])
print('取连续多列', t4[:, 2:])
print('不取连续多列', t4[:, [0, 2, 4]])
print('取行和列', t4[2, 3])
print('取多行和多列', t4[1:3, 3:])
print('取多个不相邻的点', t4[[0, 2], [0, 3]])
print('三目运算符', numpy.where(t4 < 10, 0, 10))
print('裁剪,小于10的替换成10莫大于18的替换成18', t4.clip(10, 18))
# 赋值
t4 = t4.astype('float')
t4[3, 3] = numpy.nan
print('赋值', t4)


def fill_ndarray(t1):
    for i in range(t1.shape[1]):  # 遍历每一列
        temp_clo = t1[:, i]  # 当前一列
        nan_num = numpy.count_nonzero(temp_clo != temp_clo)
        if nan_num != 0:  # 不为0,说明当前这一列中有nan
            temp_not_nan_col = temp_clo[temp_clo == temp_clo]  # 当前一列不为nan的array
            temp_clo[numpy.isnan(temp_clo)] = temp_not_nan_col.mean()

    return t1


t1 = numpy.arange(12).reshape(3, 4).astype('float')
t1[1, 2:] = numpy.nan
print('t1--',t1)
print('fill_ndarray--',fill_ndarray(t1))
